(function (angular) {
    'use strict';

    var module = angular.module('sinbad.controllers.main', ['ngResource', 'highcharts-ng', 'ui.bootstrap.modal']);

    module.controller('MainCtrl', function () {
        this.awesomeThings = [
            'HTML5 Boilerplate',
            'AngularJS',
            'Karma'
        ];
    });

    module.controller('TopCtrl', ['$scope', function ($scope) {
        $scope.account = {
            name: 'tp_十禾31',
            img: 'Img/default_avatar.jpg',
            isLogin: true
        };
        $scope.shop = {
            name: 'yyjd1123',
            img: 'Img/default_avatar.jpg'
        };
        $scope.notification = {
            count: 10,
            messages: false
        };
    }]);

    var chartopts = {
        title: {
            align: 'right',
            floating: true,
            text: null,
            style: {
                color: '#CCCCC',
                fontSize: '12px',
                fontFamily: '微软雅黑'
            }
        },
        colors: ['#88bbc8', '#ed7a53', '#9FC569', '#bbdce3', '#9a3b1b', '#5a8022', '#2c7282'],
        credits: {
            enabled: false
        },
        legend: {
            align: 'left',
            verticalAlign: 'top',
            x: -10,
            y: -15,
            borderWidth: 0,
            floating: true
        },
        plotOptions: {
            area: {
                lineWidth: 2.5,
                fillOpacity: .1,
                marker: {
                    lineColor: '#fff',
                    lineWidth: 1,
                    radius: 3.5,
                    symbol: 'circle'
                },
                shadow: false
            },
            column: {
                lineWidth: 16,
                shadow: false,
                borderWidth: 0,
                groupPadding: .05
            },
            line: {
                amination: false,
                lineWidth: 3,
                shadow: false
            }
        },
        xAxis: {
            type: 'datetime',
            title: {
                text: "时间"
            },
            //tickmarkPlacement: 'on',

            dateTimeLabelFormats: {
                day: '%m-%e'
            },

            gridLineColor: '#eeeeee',
            gridLineWidth: .5,
            labels: {
                style: {
                    color: '#999999'
                }
            }
        },
        yAxis: [{
            // sales,cost yAxis
            labels: {
                enabled: false,
                formatter: function () {
                    return "￥" + this.value;
                },
                style: {
                    color: '#89A54E'
                }
            },
            title: {
                text: null,
                style: {
                    color: '#89A54E'
                }
            },
            opposite: true

        }, {
            // ctr yAxis
            gridLineWidth: 0,
            title: {
                text: null,
                style: {
                    color: '#666666'
                }
            },
            min: 0,
            labels: {
                enabled: false,
                formatter: function () {
                    return this.value + ' %';
                },
                style: {
                    color: '#4572A7'
                }
            }

        }, { // impressions yAxis
            gridLineWidth: 0,
            title: {
                text: null,
                offset: -25,
                style: {
                    color: '#AA4643'
                }
            },
            offset: -40,
            showFirstLabel: false,
            showLastLabel: false,
            labels: {
                enabled: false,
                formatter: function () {
                    return this.value / 1000 + ' K';
                },
                style: {
                    color: '#AA4643'
                }
            },
            opposite: false
        }, {
            // cpm,cpc 3rd yAxis
            gridLineWidth: 0,
            title: {
                text: null
            },
            showFirstLabel: false,
            showLastLabel: false,
            labels: {
                enabled: false
            },
            opposite: false
        }, {
            // cpc 4rd yAxis
            gridLineWidth: 0,
            title: {
                text: null
            },
            showFirstLabel: false,
            showLastLabel: false,
            labels: {
                enabled: false
            },
            opposite: false
        }, {
            // click 5rd yAxis
            gridLineWidth: 0,
            title: {
                text: null
            },
            showFirstLabel: false,
            showLastLabel: false,
            labels: {
                enabled: false
            },
            opposite: false
        }
        ],
        tooltip: {
            shadow: false,
            xDateFormat: '%Y-%m-%d'
        }
    };


    module.controller('TotalChartCtrl', ['$scope', 'highchartsNG', function ($scope, highchartsNG) {
        var indicators = ['click', 'cpc', 'cost', 'roi'],
            label_map = {
                'cost': "花费",
                "sales": "销售额",
                "roi": "投入产出比",
                "cpc": "平均点击花费",
                "impressions": "展现量",
                "aclick": "总点击量",
                "click": "点击量",
                "ctr": "点击率",
                "cpm": "千次展现费",
                "direct_pay": "直接成交额",
                "indirect_pay": "间接成交额",
                "direct_pay_count": "直接成交笔数",
                "indirect_pay_count": "间接成交笔数",
                "fav_shop_count": "店铺收藏量",
                "fav_item_count": "商品收藏量",
                "avgpos": "平均展现排名"
            },
            data = '{"taobao_user_id":"-2252","user_id":"-2252","from":1467158400,"to":1468368000,"summary":{"cost":1617.97,"sales":3686.09,"impressions":190979,"click":4910,"pay_count":70,"roi":2.28,"cpc":0.33,"ctr":2.57,"cpm":8.47,"direct_pay":3268.33,"indirect_pay":417.76,"direct_pay_count":61,"indirect_pay_count":9,"fav_shop_count":9,"fav_item_count":228,"cvr":1.43},"series":{"\u82b1\u8d39":[[1467158400000,173.22],[1467244800000,112.24],[1467331200000,99.74],[1467417600000,99.55],[1467504000000,105.23],[1467590400000,108.5],[1467676800000,109.97],[1467763200000,108.06],[1467849600000,108.61],[1467936000000,98.32],[1468022400000,104.05],[1468108800000,101.75],[1468195200000,93.94],[1468281600000,89.08],[1468368000000,105.71]],"\u9500\u552e\u989d":[[1467158400000,202.05],[1467244800000,259.27],[1467331200000,253.67],[1467417600000,312.26],[1467504000000,346.27],[1467590400000,433.18],[1467676800000,253.65],[1467763200000,236.45],[1467849600000,236.3],[1467936000000,324.34],[1468022400000,393.45],[1468108800000,173.11],[1468195200000,42.75],[1468281600000,82.68],[1468368000000,136.66]],"\u5c55\u73b0\u91cf":[[1467158400000,18029],[1467244800000,14048],[1467331200000,11637],[1467417600000,9983],[1467504000000,10926],[1467590400000,13174],[1467676800000,12692],[1467763200000,12235],[1467849600000,11728],[1467936000000,11821],[1468022400000,11416],[1468108800000,12999],[1468195200000,14268],[1468281600000,13512],[1468368000000,12511]],"\u70b9\u51fb\u91cf":[[1467158400000,508],[1467244800000,370],[1467331200000,321],[1467417600000,320],[1467504000000,336],[1467590400000,340],[1467676800000,323],[1467763200000,328],[1467849600000,320],[1467936000000,296],[1468022400000,302],[1468108800000,301],[1468195200000,290],[1468281600000,257],[1468368000000,298]],"\u6210\u4ea4\u7b14\u6570":[[1467158400000,4],[1467244800000,6],[1467331200000,7],[1467417600000,6],[1467504000000,6],[1467590400000,8],[1467676800000,4],[1467763200000,4],[1467849600000,4],[1467936000000,5],[1468022400000,9],[1468108800000,3],[1468195200000,2],[1468281600000,1],[1468368000000,1]],"\u6295\u5165\u4ea7\u51fa\u6bd4":[[1467158400000,1.17],[1467244800000,2.31],[1467331200000,2.54],[1467417600000,3.14],[1467504000000,3.29],[1467590400000,3.99],[1467676800000,2.31],[1467763200000,2.19],[1467849600000,2.18],[1467936000000,3.3],[1468022400000,3.78],[1468108800000,1.7],[1468195200000,0.46],[1468281600000,0.93],[1468368000000,1.29]],"\u5e73\u5747\u70b9\u51fb\u82b1\u8d39":[[1467158400000,0.34],[1467244800000,0.3],[1467331200000,0.31],[1467417600000,0.31],[1467504000000,0.31],[1467590400000,0.32],[1467676800000,0.34],[1467763200000,0.33],[1467849600000,0.34],[1467936000000,0.33],[1468022400000,0.34],[1468108800000,0.34],[1468195200000,0.32],[1468281600000,0.35],[1468368000000,0.35]],"\u70b9\u51fb\u7387":[[1467158400000,2.82],[1467244800000,2.63],[1467331200000,2.76],[1467417600000,3.21],[1467504000000,3.08],[1467590400000,2.58],[1467676800000,2.54],[1467763200000,2.68],[1467849600000,2.73],[1467936000000,2.5],[1468022400000,2.65],[1468108800000,2.32],[1468195200000,2.03],[1468281600000,1.9],[1468368000000,2.38]],"\u5343\u6b21\u5c55\u73b0\u8d39":[[1467158400000,9.61],[1467244800000,7.99],[1467331200000,8.57],[1467417600000,9.97],[1467504000000,9.63],[1467590400000,8.24],[1467676800000,8.66],[1467763200000,8.83],[1467849600000,9.26],[1467936000000,8.32],[1468022400000,9.11],[1468108800000,7.83],[1468195200000,6.58],[1468281600000,6.59],[1468368000000,8.45]],"\u76f4\u63a5\u6210\u4ea4\u989d":[[1467158400000,162.51],[1467244800000,153.81],[1467331200000,253.67],[1467417600000,312.26],[1467504000000,346.27],[1467590400000,433.18],[1467676800000,253.65],[1467763200000,173.74],[1467849600000,216.52],[1467936000000,324.34],[1468022400000,353.51],[1468108800000,22.78],[1468195200000,42.75],[1468281600000,82.68],[1468368000000,136.66]],"\u95f4\u63a5\u6210\u4ea4\u989d":[[1467158400000,39.54],[1467244800000,105.46],[1467331200000,0],[1467417600000,0],[1467504000000,0],[1467590400000,0],[1467676800000,0],[1467763200000,62.71],[1467849600000,19.78],[1467936000000,0],[1468022400000,39.94],[1468108800000,150.33],[1468195200000,0],[1468281600000,0],[1468368000000,0]],"\u76f4\u63a5\u6210\u4ea4\u7b14\u6570":[[1467158400000,3],[1467244800000,3],[1467331200000,7],[1467417600000,6],[1467504000000,6],[1467590400000,8],[1467676800000,4],[1467763200000,3],[1467849600000,3],[1467936000000,5],[1468022400000,8],[1468108800000,1],[1468195200000,2],[1468281600000,1],[1468368000000,1]],"\u95f4\u63a5\u6210\u4ea4\u7b14\u6570":[[1467158400000,1],[1467244800000,3],[1467331200000,0],[1467417600000,0],[1467504000000,0],[1467590400000,0],[1467676800000,0],[1467763200000,1],[1467849600000,1],[1467936000000,0],[1468022400000,1],[1468108800000,2],[1468195200000,0],[1468281600000,0],[1468368000000,0]],"\u5e97\u94fa\u6536\u85cf\u91cf":[[1467158400000,1],[1467244800000,2],[1467331200000,0],[1467417600000,0],[1467504000000,2],[1467590400000,0],[1467676800000,0],[1467763200000,1],[1467849600000,0],[1467936000000,1],[1468022400000,0],[1468108800000,0],[1468195200000,1],[1468281600000,0],[1468368000000,1]],"\u5546\u54c1\u6536\u85cf\u91cf":[[1467158400000,20],[1467244800000,24],[1467331200000,21],[1467417600000,17],[1467504000000,12],[1467590400000,21],[1467676800000,12],[1467763200000,20],[1467849600000,8],[1467936000000,10],[1468022400000,12],[1468108800000,10],[1468195200000,24],[1468281600000,8],[1468368000000,9]],"\u70b9\u51fb\u8f6c\u5316\u7387":[[1467158400000,0.79],[1467244800000,1.62],[1467331200000,2.18],[1467417600000,1.88],[1467504000000,1.79],[1467590400000,2.35],[1467676800000,1.24],[1467763200000,1.22],[1467849600000,1.25],[1467936000000,1.69],[1468022400000,2.98],[1468108800000,1],[1468195200000,0.69],[1468281600000,0.39],[1468368000000,0.34]]}}';
        data = $.parseJSON(data);
        var data_cache = {};
        for (var label in data.series) {
            var dao = {};
            switch (label) {
                case '花费':
                case '销售额':
                    dao.type = 'line';
                    dao.yAxis = 0;
                    break;

                case '展现量':
                    dao.type = 'line';
                    dao.yAxis = 5;
                    break;
                case '点击量':
                    dao.type = 'line';
                    dao.yAxis = 2;
                    break;

                case '点击率':
                case '投入产出比':
                    dao.type = 'line';
                    dao.yAxis = 1;
                    break;

                case '千次展现费':
                    dao.type = 'line';
                    dao.yAxis = 3;
                    break;

                case '平均点击花费':
                    dao.type = 'line';
                    dao.yAxis = 4;
                    break;

                default:
                    continue;
                    break;
            }
            dao.name = label;
            dao.data = data.series[label];
            data_cache[label] = dao;

            var default_data = [];
            for (var ix in indicators) {
                var label = label_map[indicators[ix]];
                default_data.push(data_cache[label]);
            }

            highchartsNG.ready(function () {
                $scope.chartConfig = $.extend({
                    title: {
                        text: '走势图'
                    },
                    series: default_data
                }, chartopts);
            }, this);

        }
    }]);

    module.controller('CampaignsCtrl', ['$scope', '$uibModal',function($scope, $modal){
        $scope.campaign = {
            autoType: 2
        };
        $scope.openCampaignEditor = function(){
            var modalInstance = $modal.open({
                templateUrl: 'views/main/campaign_editor.html',
                controller: 'CampaignEditorCtrl',
                size: 200,
                resolve: {
                    campaign: function () {
                        return $scope.campaign;
                    }
                }
            });

            modalInstance.result.then(function (campaign) {
                console.log('campaign', campaign);
                $scope.campaign = campaign;
            }, function () {

            });
        };
    }]);

    module.controller('CampaignEditorCtrl', ['$scope', '$uibModalInstance', 'campaign',
        function($scope, $uibModalInstance, campaign){
            $scope.campaign = campaign;
            $scope.ok = function(){
                $uibModalInstance.close($scope.campaign);
            };
            $scope.cancel = function () {
                $uibModalInstance.dismiss('cancel');
            };
    }]);

    module.controller("main.TrendCtrl", ["$scope", function ($scope) {
        $scope.impressions_sum=2255.5;
        $scope.show_sum=14145;
        $scope.click_sum=7145;
        $scope.trade_sum=43145;
        $scope.trade_sum_vol=145;
        $scope.ratio=14145;
        $scope.expenses=6754;
    }]);

    module.controller('CampaignsCtrl', ['$scope', '$uibModal',function($scope, $modal){
        $scope.campaign = {
            autoType: 2
        };
        $scope.openCampaignEditor = function(){
            var modalInstance = $modal.open({
                templateUrl: 'views/main/campaign_editor.html',
                controller: 'CampaignEditorCtrl',
                size: 200,
                resolve: {
                    campaign: function () {
                        return $scope.campaign;
                    }
                }
            });

            modalInstance.result.then(function (campaign) {
                console.log('campaign', campaign);
                $scope.campaign = campaign;
            }, function () {

            });
        };
    }]);


})(angular);
